package com.ymttest.business.service;

import org.json.JSONArray;
import org.json.JSONObject;

import com.ymatou.iapi.shoppingcart.parameter.AddShoppingCartBean;
import com.ymatou.iapi.shoppingcart.parameter.ClearInvalidProductBean;
import com.ymatou.iapi.shoppingcart.parameter.GetShoppingCartListBean;
import com.ymatou.iapi.shoppingcart.service.AddShoppingCartCall;
import com.ymatou.iapi.shoppingcart.service.ClearInvalidProductCall;
import com.ymatou.iapi.shoppingcart.service.GetShoppingCartListCall;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.utils.EnvSetup;

public class BuyerAppShoppingCartService {
	
	private static AddShoppingCartBean addShoppingCartBean = new AddShoppingCartBean();
	private static AddShoppingCartCall addShoppingCartCall = new AddShoppingCartCall();
	
	private static GetShoppingCartListBean getShoppingCartListBean = new GetShoppingCartListBean();
	private static GetShoppingCartListCall getShoppingCartListCall = new GetShoppingCartListCall();
	
	private static ClearInvalidProductBean clearInvalidProductBean = new ClearInvalidProductBean();
	private static ClearInvalidProductCall clearInvalidProductCall = new ClearInvalidProductCall();
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("zhangyi_seller002_id"));
	private static String sellerName = EnvSetup.getData("zhangyi_seller002_name");
	
	public BuyerAppShoppingCartService() {
	}
	
	/***
	 * 添加商品至购物车
	 * @param productId
	 * @param buyerUesrId
	 * @param purchaseNum
	 * @return
	 * @throws Exception
	 */
	public JSONObject addShoppingCart(String productId, int buyerUserId, int purchaseNum) throws Exception {
		 
		ProductDetailModelWapper wapper = new ProductDetailModelWapper();
		JSONArray catalogList = new JSONArray(wapper.getProductId(productId).get("CatalogList").toString());
		
		//默认添加第一个规格
		String catalogId = new JSONObject(catalogList.get(0).toString()).getString("CatalogId");
		
		addShoppingCartBean.setCatalogId(catalogId);
		addShoppingCartBean.setUserId(buyerUserId);
		addShoppingCartBean.setPurchaseNum(purchaseNum);
		addShoppingCartBean.setSource(0);
		addShoppingCartBean.setClient(0);
		 
		addShoppingCartCall.setData(addShoppingCartBean);
		addShoppingCartCall.callService();
		
		return new JSONObject (addShoppingCartCall.getReturnData());
	}
	
	/***
	 * 刷新购物车列表
	 * @param buyerUesrId
	 * @return
	 * @throws Exception
	 */
	public JSONObject getShoppingCart(int buyerUserId) throws Exception {

		 getShoppingCartListBean.setExcludeSellerCoupon(false);
		 getShoppingCartListBean.setGroupByPromotion(true);
		 getShoppingCartListBean.setUserId(4085);
		 
		 getShoppingCartListCall.setData(getShoppingCartListBean);
		 getShoppingCartListCall.callService();
		
		return new JSONObject (addShoppingCartCall.getReturnData());
	}
	
	/***
	 * 清空购物车失效商品列表
	 * @param buyerUesrId
	 * @return
	 * @throws Exception
	 */
	public JSONObject clearInvalidProductList(int buyerUserId) throws Exception {

		clearInvalidProductBean.setUserId(buyerUserId);
		 
		clearInvalidProductCall.setData(clearInvalidProductBean);
		clearInvalidProductCall.callService();
		
		return new JSONObject (clearInvalidProductCall.getReturnData());
	}
	
	public static void main (String[] args) throws Exception {
		for (int i = 0; i < 50; i++) {
			String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			BuyerAppShoppingCartService service = new BuyerAppShoppingCartService();
			service.addShoppingCart(productId, 20324921, 1);
		}
	}
}
